package org.example.travel_web.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.example.travel_web.dto.PageResult;
import org.example.travel_web.dto.comment.*;
import org.example.travel_web.entity.*;
import org.example.travel_web.enums.Type;
import org.example.travel_web.exception.RunException;
import org.example.travel_web.mapper.CommentsMapper;
import org.example.travel_web.service.AttractionsService;
import org.example.travel_web.service.CommentsService;
import org.example.travel_web.service.NewsService;
import org.example.travel_web.service.RoutesService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 23591
 * @description 针对表【travel_comments(用于存储用户的评论信息)】的数据库操作Service实现
 * @createDate 2025-01-22 14:10:11
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, CommentsET>
        implements CommentsService {
    @Resource
    private AttractionsService attractionsService;
    @Resource
    private NewsService newsService;
    @Resource
    private RoutesService routesService;

    @Override
    public PageResult<CommentPageResp> pageResult(PageReq pageReq) {
        MPJLambdaWrapper<CommentsET> wrapper = new MPJLambdaWrapper<CommentsET>()
                .selectAll(CommentsET.class)
                .selectAll(UserET.class);
        wrapper.eq(CommentsET::getType, pageReq.getType());
        wrapper.leftJoin(UserET.class, UserET::getUid, CommentsET::getUid);
        wrapper.orderByDesc(CommentsET::getCreatedAt);
        switch (pageReq.getType()) {
            case NEWS -> {
                wrapper.selectAs(NewsET::getTitle, CommentPageResp::getRelateName);
                wrapper.rightJoin(NewsET.class, NewsET::getArticleId, CommentsET::getRelateId);
            }
            case ROUTE -> {
                wrapper.selectAs(RoutesET::getTitle, CommentPageResp::getRelateName);
                wrapper.rightJoin(RoutesET.class, RoutesET::getRouteId, CommentsET::getRelateId);
            }
            case ATTRACTION -> {
                wrapper.selectAs(AttractionsET::getName, CommentPageResp::getRelateName);
                wrapper.rightJoin(AttractionsET.class, AttractionsET::getAttractionId, CommentsET::getRelateId);
            }
        }
        Page<CommentPageResp> page = getBaseMapper().selectJoinPage(new Page<>(pageReq.getPage(), pageReq.getSize()), CommentPageResp.class, wrapper);
        return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), page.getRecords());
    }

    @Override
    public CommentDetail getCommentDetail(Integer id) {
        MPJLambdaWrapper<CommentsET> wrapper = new MPJLambdaWrapper<CommentsET>()
                .selectAll(CommentsET.class)
                .selectAll(UserET.class);
        wrapper.eq(CommentsET::getCommentId, id);
        wrapper.leftJoin(UserET.class, UserET::getUid, CommentsET::getUid);
        CommentDetail commentDetail = getBaseMapper().selectJoinOne(CommentDetail.class, wrapper);
        if (commentDetail == null) {
            throw new RunException("评论不存在");
        }
        switch (Type.valueOf(commentDetail.getType())) {
            case NEWS -> {
                LambdaQueryWrapper<NewsET> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(NewsET::getArticleId, commentDetail.getRelateId());
                NewsET newsET = newsService.getOne(queryWrapper);
                commentDetail.setRelateName(newsET.getTitle());
            }
            case ROUTE -> {
                LambdaQueryWrapper<RoutesET> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(RoutesET::getRouteId, commentDetail.getRelateId());
                RoutesET routesET = routesService.getOne(queryWrapper);
                commentDetail.setRelateName(routesET.getTitle());
            }
            case ATTRACTION -> {
                LambdaQueryWrapper<AttractionsET> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(AttractionsET::getAttractionId, commentDetail.getRelateId());
                AttractionsET attractionsET = attractionsService.getOne(queryWrapper);
                commentDetail.setRelateName(attractionsET.getName());
            }
        }
        return commentDetail;
    }

    @Override
    public void deleteComment(Integer[] ids) {
        if (!removeByIds(List.of(ids))) {
            throw new RunException("删除失败");
        }
    }

    @Override
    public void updateCommentStatus(Integer id) {
        LambdaQueryWrapper<CommentsET> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CommentsET::getCommentId, id);
        CommentsET commentsET = getOne(queryWrapper);
        if (commentsET == null) {
            throw new RunException("评论不存在");
        }
        LambdaUpdateWrapper<CommentsET> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CommentsET::getCommentId, id);
        updateWrapper.set(CommentsET::getStatus, commentsET.getStatus() == 0 ? 1 : 0);
        if (!update(updateWrapper)) {
            throw new RunException("修改失败");
        }
    }

    @Override
    public PageResult<UserCommentPageResp> userCommentPageResult(UserCommentPageReq req) {
        MPJLambdaWrapper<CommentsET> wrapper = new MPJLambdaWrapper<CommentsET>()
                .selectAll(CommentsET.class)
                .selectAll(UserET.class);
        wrapper.eq(CommentsET::getType, req.getType());
        wrapper.eq(CommentsET::getRelateId, req.getRelateId());
        wrapper.leftJoin(UserET.class, UserET::getUid, CommentsET::getUid);
        wrapper.orderByDesc(CommentsET::getCreatedAt);
        Page<UserCommentPageResp> page = getBaseMapper().selectJoinPage(new Page<>(req.getPage(), req.getSize()), UserCommentPageResp.class, wrapper);
        return PageResult.of(page.getTotal(), page.getCurrent(), page.getSize(), page.getRecords());
    }

    @Override
    public void addComment(CommentReq req) {
        CommentsET bean = BeanUtil.toBean(req, CommentsET.class);
        bean.setUid(StpUtil.getLoginIdAsLong());
        if (!save(bean)) {
            throw new RunException("添加失败");
        }
    }
}




